<!DOCTYPE html>
<html>
	<head>
		<title>设置个人资料</title>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
		<link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
		<script src="/js/jquery/jquery.min.js"></script>
		<script src="/js/bootstrap/js/bootstrap.min.js"></script>
		<link rel="stylesheet" href="/css/font/css/font-awesome.min.css" />
		<link rel="stylesheet" href="/css/reset.css" />
		<link rel="stylesheet" href="/css/myInfo.css" />
		<script src="/js/plugins/jrender/jrender.js"></script>
		<script src="/js/plugins/jquery-form/jquery.form.js"></script>
		<script type="text/javascript">
			$(function () {
			    var struser=sessionStorage.getItem("user");
			    var user=JSON.parse(struser);
			    $(".info").renderValues(user);
                console.log(user);

                //回显背景
				$(".myBg").css("background","url('"+user.coverImgUrl+"')");
				//保存
				$("#saveBtn").click(function () {
					$("#infoForm").ajaxSubmit({
						url:"/users/"+user.id,
						success:function (data) {
                            data.obj.email=user.email;
                            //将当前修改后的用户重新共享到sessionStorage中
							var struser=JSON.stringify(data.obj);
						    sessionStorage.setItem("user",struser);
							location.href="/mine/profiles.html";

                        }
					})
                })


                //
				var type;  //0:头像  1：背景
				$("#headBtn,#coverBtn").click(function () {
				    //获取需要操作的步骤
					type=$(this).data("type");
					$("#imgForm :file").click();
                })

				//值改变事件
				$("#imgForm :file").change(function () {
                    console.log(this);
                    if (this.value) {
                        //上传图片
                        $("#imgForm").ajaxSubmit(function (data) {
							if (type==0){
							    //回显头像
                                $("#headImg").attr("src",data.url);
                                $("#infoForm input[name='headImgUrl']").val(data.url);
							}else {
							    //回显背景
                                $(".myBg").css("background","url("+data.url+")");
                                $("#infoForm input[name='coverImgUrl']").val(data.url);
							}
                        })
                    }
                })

            });
		</script>
	</head>

	<body>
		<form id="imgForm" action="/images" method="post" enctype="multipart/form-data">
			<input type="file" name="pic" style="display: none;" accept="image/*"/>
		</form>

		<div class="search-head">
			<div class="row nav-search">
				<div class="col">
					<a href="javascript:history.back();">
						<span>取消</span>
					</a>
				</div>
				<div class="col">
					<span>设置个人资料</span>
				</div>
				<div class="col" >
					<a href="javascript:" id="saveBtn" >
						<span>保存</span>
					</a>
				</div>
			</div>
		</div>

		<div class="container info">
			<div class="myBg">
				<img id="headImg"  class="rounded-circle" render-src="headImgUrl"/>
				<div class="editHead" id="headBtn" data-type="0">
					<i class="fa fa-pencil-square fa-lg " aria-hidden="true"></i>
				</div>
				<button  class="btn btn-outline-light ibtn " id="coverBtn" data-type="1">设置背景</button>
			</div>

			<form id="infoForm" method="post" >
				<!--改变method的请求方式-->
				<input type="hidden" name="_method" value="PUT">
				<!--隐藏的数据用于上面的数据-->
				<input type="hidden" name="headImgUrl" render-value="headImgUrl">
				<input type="hidden" name="coverImgUrl" render-value="coverImgUrl">
				<label>昵称</label>
				<input type="text" name="nickName" class="form-control" render-value="nickName">
				<label>性别</label>
				<select class="form-control" name="gender" render-value="gender">
					<option value="0">保密</option>
					<option value="1">男</option>
					<option value="2">女</option>
				</select>
				<label>地区</label>
				<input type="text" name="place"  class="form-control" render-value="place">
				<label>签名</label>
				<textarea name="sign" class="form-control" rows="5" render-html="sign"></textarea>
			</form>
		</div>

	</body>
</html>
